package pri.zxx.excelgenerate.dataentity.tabledata.opeconomicbenefit;

import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import pri.zxx.excelgenerate.dataentity.entity.CreatRequest;
import pri.zxx.excelgenerate.dataentity.entity.ModuleInfoEnum;
import pri.zxx.excelgenerate.dataentity.entity.ModuleMetaInfo;
import pri.zxx.excelgenerate.dataentity.tabledata.MultiTitleModuleCreatorTemplate;
import pri.zxx.excelgenerate.dataentity.tabledata.RangeVo;
import pri.zxx.excelgenerate.dataentity.tabledata.TableDefinition;
import pri.zxx.excelgenerate.excelhanler.ExcelUtil;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 妊娠母猪
 */
@EqualsAndHashCode(callSuper = true)
@Accessors(chain = true)
@Component
@Slf4j
public class EconomicBenefitRangeModuleCreator extends MultiTitleModuleCreatorTemplate {

    //todo 注入数据获取的bean

    @Override
    public List<ModuleMetaInfo> creator(CreatRequest creatRequest) {
        CreatRequest.TableCreateReq tcr = new CreatRequest.TableCreateReq(4.0, 1, null);
        creatRequest.setTableCreateReq(tcr);

        List<ModuleMetaInfo> modules = new ArrayList<>();
        List<CreatRequest.Range> ranges = creatRequest.getRanges();
        //有范围要求的表格 根据选择的范围拆分 向上取整
        int tableCount = (int) Math.ceil(ranges.size() / tcr.getMAX_REPEAT());//创建module数量

        List<RangeVo> unLoadData = new ArrayList<>();

        switch (creatRequest.getPredictionTypeEnum()) {
            case GESTATING_SOWS:
            case STARTING_PIGS:
            case LACTATING_SOWS:
                log.warn("种类：【{} 】不存在【{}】模块", creatRequest.getPredictionTypeEnum().getValue(), moduleInfo().getName());
                return null;
            case GROWING_FINISHING:
                //todo 调用获取数据的service
                // 模拟数据
                EconomicBenefitRangeGrowingFinishing op = new EconomicBenefitRangeGrowingFinishing();
                EconomicBenefitRangeGrowingFinishing op2 = new EconomicBenefitRangeGrowingFinishing();
                EconomicBenefitRangeGrowingFinishing op3 = new EconomicBenefitRangeGrowingFinishing();
                EconomicBenefitRangeGrowingFinishing op4 = new EconomicBenefitRangeGrowingFinishing();
                op.setInitialBwforaa(BigDecimal.valueOf(1)).setFinalBwforaa(BigDecimal.valueOf(30)).setInitialDayinput(1).setFinalDayinput(30);
                op2.setInitialBwforaa(BigDecimal.valueOf(30)).setFinalBwforaa(BigDecimal.valueOf(50)).setInitialDayinput(30).setFinalDayinput(50);
                op3.setInitialBwforaa(BigDecimal.valueOf(50)).setFinalBwforaa(BigDecimal.valueOf(60)).setInitialDayinput(50).setFinalDayinput(60);
                op4.setInitialBwforaa(BigDecimal.valueOf(60)).setFinalBwforaa(BigDecimal.valueOf(80)).setInitialDayinput(60).setFinalDayinput(80);
                unLoadData.add(op);
                unLoadData.add(op2);
                unLoadData.add(op3);
                break;
            default:
                throw new RuntimeException("动物类型异常");

        }

//        opEconomicBenefits.add(op4);

        for (int j = 0; j < tableCount; j++) {

            ModuleMetaInfo metaInfo = new ModuleMetaInfo();
            TableDefinition tableDefinition = new TableDefinition();

            List<CreatRequest.Range> rg = ranges.stream().skip((long) tcr.getMAX_REPEAT() * j).limit((long) tcr.getMAX_REPEAT()).collect(Collectors.toList());

            List<RangeVo> opEconomicBenefitsListRange = unLoadData.stream().filter(opVitamin -> {
                //判断是体重范围 还是 时间范围
                Boolean isWeight = ExcelUtil.isWeight(creatRequest);
                String rangeStr;
                if (isWeight) {
                    BigDecimal finalBwforaa = opVitamin.getFinalBwforaa();
                    BigDecimal initialBwforaa = opVitamin.getInitialBwforaa();
                    rangeStr = initialBwforaa.toString() + "-" + finalBwforaa.toString();
                } else {
                    Integer initialDayinput = opVitamin.getInitialDayinput();
                    Integer finalDayinput = opVitamin.getFinalDayinput();
                    rangeStr = initialDayinput + "-" + finalDayinput;
                }
                return rg.stream().anyMatch(range -> rangeStr.equals(range.getStart() + "-" + range.getEnd()));
            }).collect(Collectors.toList());

            tableDefinition.setUnLoadData(opEconomicBenefitsListRange)
                    .setDataClass(EconomicBenefitRangeGrowingFinishing.class);

            CreatRequest cr = new CreatRequest();
            BeanUtils.copyProperties(creatRequest, cr);
            cr.setRanges(rg);

            //组装数据
            tableDefinition = dataHandle(moduleMetaInfoHandle(tableDefinition, cr), cr);
            metaInfo.setTableDefinition(tableDefinition);

            metaInfo.setModuleTitle(moduleInfo().getName() + JOIN_DECILITER + (j + 1)).setModuleInfoEnum(moduleInfo());
            modules.add(metaInfo);
        }
        return modules;
    }

    @Override
    public ModuleInfoEnum moduleInfo() {
        return ModuleInfoEnum.OP_EONOMIC_BENEFIT_RANGE;
    }

}
